package com.ruoyi.finance.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.IndexDtoByTwo;
import com.ruoyi.caigou.api.RemoteBuyOrderService;
import com.ruoyi.caigou.domain.BuyOrder;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.store.api.RemoteStockInXService;
import com.ruoyi.system.api.RemoteStockInService;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.xiaoshou.api.RemoteSaleBackService;
import com.ruoyi.xiaoshou.domain.SaleBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.BuyMustPayMapper;
import com.ruoyi.finance.domain.BuyMustPay;
import com.ruoyi.finance.service.IBuyMustPayService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购应付Service业务层处理
 *
 * @author lvql
 * @date 2024-11-06
 */
@Service
public class BuyMustPayServiceImpl implements IBuyMustPayService {
    @Autowired
    private BuyMustPayMapper buyMustPayMapper;

    @Autowired
    private RemoteStockInXService remoteStockInXService;

    @Autowired
    private RemoteStockInService remoteStockInService;

    @Autowired
    private RemoteBuyOrderService remoteBuyOrderService;

    @Autowired
    private RemoteSaleBackService remoteSaleBackService;
    /**
     * 查询采购应付
     *
     * @param id 采购应付主键
     * @return 采购应付
     */
    @Override
    public BuyMustPay selectBuyMustPayById(Integer id) {
        return buyMustPayMapper.selectBuyMustPayById(id);
    }

    /**
     * 查询采购应付列表
     *
     * @param buyMustPay 采购应付
     * @return 采购应付
     */
    @Override
    public List<BuyMustPay> selectBuyMustPayList(BuyMustPay buyMustPay) {
        return buyMustPayMapper.selectBuyMustPayList(buyMustPay);
    }

    @Override
    public List<BuyMustPay> getReconList(BuyMustPay buyMustPay) {
        return buyMustPayMapper.getReconList(buyMustPay);
    }

    @Override
    public BigDecimal getAllMustPay() {
        return buyMustPayMapper.getAllMustPay();
    }

    @Override
    public IndexDtoByTwo getBuyCountAndMoney() {
        return buyMustPayMapper.getBuyCountAndMoney();
    }

    @Override
    public IndexDtoByTwo getSaleBackCountAndMoney() {
        return buyMustPayMapper.getSaleBackCountAndMoney();
    }

    /**
     * 新增采购应付
     *
     * @param buyMustPay 采购应付
     * @return 结果
     */
    @Override
    public int insertBuyMustPay(BuyMustPay buyMustPay) {
        buyMustPay.setCreateTime(DateUtils.getNowDate());
        return buyMustPayMapper.insertBuyMustPay(buyMustPay);
    }

    /**
     * 修改采购应付
     *
     * @param buyMustPay 采购应付
     * @return 结果
     */
    @Override
    public int updateBuyMustPay(BuyMustPay buyMustPay) {
        buyMustPay.setUpdateTime(DateUtils.getNowDate());
        return buyMustPayMapper.updateBuyMustPay(buyMustPay);
    }

    /**
     * 批量删除采购应付
     *
     * @param ids 需要删除的采购应付主键
     * @return 结果
     */
    @Override
    public int deleteBuyMustPayByIds(Integer[] ids) {
        return buyMustPayMapper.deleteBuyMustPayByIds(ids);
    }

    /**
     * 删除采购应付信息
     *
     * @param id 采购应付主键
     * @return 结果
     */
    @Override
    public int deleteBuyMustPayById(Integer id) {
        return buyMustPayMapper.deleteBuyMustPayById(id);
    }

    @Override
    public List<BuyMustPay> selectBuyMustPayListBySourceCodeList(List<String> rkCodeList) {
        return buyMustPayMapper.selectBuyMustPayListBySourceCodeList(rkCodeList);
    }


    /**/
    @Transactional
    @Override
    public int verified(BuyMustPay buyMustPay, BigDecimal requestAmount) {
        BigDecimal waitPayAmount = buyMustPay.getTotalAmount().subtract(buyMustPay.getVerifiedAmount());// 待支付金额
        if (requestAmount.compareTo(waitPayAmount) > 0) {
            return 0;
        }

        buyMustPay.setVerifiedAmount(buyMustPay.getVerifiedAmount().add(requestAmount));
        String sourceCode = buyMustPay.getSourceCode();// 入库单号
        //更新入库单
        R<Boolean> r = remoteStockInXService.updateStockInPaidAmount(sourceCode, requestAmount, SecurityConstants.INNER);
        if (r.getCode() == R.FAIL) {
            throw new ServiceException("更新入库失败");
        }
        //更新订单
        //通过入库单号 查找对应订单号
        R<StockIn> stockInByCode = remoteStockInService.getStockInByCode(sourceCode, SecurityConstants.INNER);
        if (r.getCode() == R.FAIL) {
            throw new ServiceException("查找入库单失败");
        }
        StockIn stockIn = stockInByCode.getData();
        String orderCode = stockIn.getOrderCode();//采购订单号
        if (stockIn.getInventoryType().equals(1)) {
            R<BuyOrder> buyOrderR = remoteBuyOrderService.getInfoByCode(orderCode, SecurityConstants.INNER);
            if (buyOrderR.getCode() == R.FAIL) {
                throw new ServiceException("查找采购订单失败");
            }
            BuyOrder buyOrder = buyOrderR.getData();
            buyOrder.setCode(orderCode);
            buyOrder.setPaidAmount(buyOrder.getPaidAmount().add(requestAmount));

            buyOrder.setPayStatus(
                    buyOrder.getTotalAmount().compareTo(buyOrder.getPaidAmount()) == 0 ? 2 : 1
            );

            R<Boolean> booleanR = remoteBuyOrderService.innerEditByCode(buyOrder, SecurityConstants.INNER);
            if (booleanR.getCode() == R.FAIL) {
                throw new ServiceException("更新采购订单失败");
            }
        }else if (stockIn.getInventoryType().equals(4)) {
            R<SaleBack> saleBackByCode = remoteSaleBackService.getSaleBackByCode(orderCode);
//            R<BuyOrder> buyOrderR = remoteSaleBackService.getInfoByCode(orderCode, SecurityConstants.INNER);
            if (saleBackByCode.getCode() == R.FAIL) {
                throw new ServiceException("查找销售退货订单失败");
            }
            SaleBack saleBack = saleBackByCode.getData();
            saleBack.setPayMoney(
                    saleBack.getPayMoney() == null? requestAmount: saleBack.getPayMoney().add(requestAmount)
            );
//            data.setPaidAmount(buyOrder.getPaidAmount().add(requestAmount));

            saleBack.setPayStatus(
                    saleBack.getRealMoney().compareTo(saleBack.getPayMoney()) == 0 ? 2 : 1
            );

            R<Boolean> booleanR = remoteSaleBackService.innerEditByCode(saleBack,SecurityConstants.INNER);
            if (booleanR.getCode() == R.FAIL) {
                throw new ServiceException("更新销售退货单失败");
            }
        }
        return buyMustPayMapper.updateBuyMustPay(buyMustPay);
    }
}
